package ru.susu.algebra.centralunits.alternating.initializers;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.centralunits.exponent.ExponentUtils;
import ru.susu.algebra.pair.Pair;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 10.09.2010
 */
public class ExponentsInitializer extends MathMethodWithInitializers<Object>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class};

	private static String PARTITION_2_EXPONENT = "Partition2Exponent";
	@Override
	protected Object directRun(IPropertySource ps) throws Exception
	{
		if (!ps.containsKey(PARTITION_2_EXPONENT))
		{
			ps.setValue(PARTITION_2_EXPONENT, ExponentUtils.partition2Exponent(SpecialRowsInitializer.listSpecialNotIntegerCharacterRows(ps)));
		}
		return null;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

	@SuppressWarnings("unchecked")
	private static Map<Partition, List<Pair<Integer, BigInteger>>> getPartition2Exponent(IPropertySource ps)
	{
		return (Map<Partition, List<Pair<Integer, BigInteger>>>)ps.getValue(PARTITION_2_EXPONENT);
	}

	public static List<Pair<Integer, BigInteger>> getExponents4Partition(IPropertySource ps, Partition partition)
	{
		return getPartition2Exponent(ps).get(partition);
	}

	public static BigInteger getLcmExponent4Partition(IPropertySource ps, Partition partition)
	{
		return ExponentUtils.getExponent(getExponents4Partition(ps, partition));
	}

}
